package com.demo2do.fms.entity.supplier.ware;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.format.PersistentFormat;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 * 仓储的服务供方 单独字段
 * 
 * 与supplier的关联关系：一对一
 * Created by zhonghui on 2017/7/13.
 */
@Entity
@Table(name = "FMS_WARE_SUPPLIER")
@DynamicUpdate
public class WareSupplier {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
    @SequenceGenerator(name = "generator", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1)
    private Long id;

    /** 
     * 与supplier关联 
     */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @PersistentFormat
    private Supplier supplier;

    /**
     * 参与运作仓库
     */
    private String operationWarehouse;

    /**
     * 参与运作码头
     */
    private String operationDock;

    /**
     * 参与运作站台
     */
    private String operationPlatform;

    /**
     * 仓储类型
     */
    @Enumerated(EnumType.ORDINAL)
    private WareType wareType;

    /**
     * 维修资质上传
     */
    private String maintenanceQualification;
    
    /**
     * 现场人员清单上传
     */
    private String onsiteStaffList;

    /**
     * 商检许可证原件
     */
    private String inspectionPermit;

    /** 创建者 */
    private Long creator;
    
    /** 创建时间 */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /** 最后修改者 */
    private Long lastOperator;

    /** 最近修改时间 */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastModifyTime;
    
    /** 是否注销 */
    @Column(name = "IS_DISABLED")
    private Boolean disabled;

    /**
     * The default constructor
     */
    public WareSupplier() {
    }

    /**
     * 
     * @param supplier
     */
    public WareSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    /**
     * @param operator
     * @return
     */
    public WareSupplier initialize(Long operator) {
        this.creator = operator;
        this.createTime = new Date();
        this.lastModifyTime = new Date();
        this.lastOperator = operator;
        this.disabled = false;
        return this;
    }
    
    /**
     * 
     * @param loginId
     * @param operationPlatform
     * @param operationDock
     * @param operationWarehouse
     * @return
     */
    public WareSupplier edit(Long loginId, List<String> operationPlatform, List<String> operationDock, List<String> operationWarehouse) {
        
        this.operationPlatform = StringUtils.join(operationPlatform, ",");
        this.operationDock = StringUtils.join(operationDock, ",");
        this.operationWarehouse = StringUtils.join(operationWarehouse, ",");
        this.lastModifyTime = new Date();
        this.lastOperator = loginId;
        return this;
    }
    
    /**
     * @param operator
     * @param now
     * @return
     */
    public WareSupplier disable(Long operator) {
        this.disabled = true;
        this.lastOperator = operator;
        this.lastModifyTime = new Date();
        return this;
    }

    /**
     * 
     * @return
     */
    @Transient
    public boolean isValid() {
        return StringUtils.isNoneBlank(this.onsiteStaffList) &&
               (this.disabled == false);
        
    }
    
    /**
     * @return
     */
    public String inValidMessage() {
    	
        if (StringUtils.isBlank(onsiteStaffList)) {
            return "现场人员清单未上传";
        } else if (disabled) {
            return "该仓储供方无效";
        } else {
            return null;
        }
        
    }
    
    /**
     * @return
     */
    public String getOperationDockDesc() {
        
        if(StringUtils.isEmpty(this.operationDock)) {
            return null;
        }
        
        String[] operationDocks = StringUtils.split(this.operationDock, ",");
        List<String> descs = new ArrayList<String>();
        for(String operationDockString : operationDocks) {
            if(WareDock.codeOf(operationDockString) != null) {
                descs.add(WareDock.codeOf(operationDockString).getDescription());
            }
        }
        
        return StringUtils.join(descs, "、");
    }
    
    /**
     * 
     * @return
     */
    public String getOperationPlatformDesc() {
        
        if(StringUtils.isEmpty(this.operationPlatform)) {
            return null;
        }
        
        String[] operationPlatforms = StringUtils.split(this.operationPlatform, ",");
        List<String> descs = new ArrayList<String>();
        for(String operationPlatformString : operationPlatforms) {
            if(WarePlatform.codeOf(operationPlatformString) != null) {
                descs.add(WarePlatform.codeOf(operationPlatformString).getDescription());
            }
        }
        
        return StringUtils.join(descs, "、");
    }

    /**
     * Gets id.
     *
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * Gets supplier .
     *
     * @return the supplier 
     */
    public Supplier getSupplier() {
        return supplier;
    }

    /**
     * Gets operation warehouse.
     *
     * @return the operation warehouse
     */
    public String getOperationWarehouse() {
        return operationWarehouse;
    }

    /**
     * Gets operation dock.
     *
     * @return the operation dock
     */
    public String getOperationDock() {
        return operationDock;
    }

    /**
     * Gets operation platform.
     *
     * @return the operation platform
     */
    public String getOperationPlatform() {
        return operationPlatform;
    }

    /**
     * 
     * @return
     */
    public WareType getWareType() {
        return wareType;
    }

    /**
     * Gets maintenance qualification.
     *
     * @return the maintenance qualification
     */
    public String getMaintenanceQualification() {
        return maintenanceQualification;
    }

    /**
     * Gets onsite staff list.
     *
     * @return the onsite staff list
     */
    public String getOnsiteStaffList() {
        return onsiteStaffList;
    }

    /**
     * Gets inspection permit.
     *
     * @return the inspection permit
     */
    public String getInspectionPermit() {
        return inspectionPermit;
    }

    /**
     * Gets disabled.
     *
     * @return the disabled
     */
    public Boolean getDisabled() {
        return disabled;
    }

    /**
     * Gets create time.
     *
     * @return the create time
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * Gets creator.
     *
     * @return the creator
     */
    public Long getCreator() {
        return creator;
    }

    /**
     * Gets last modify time.
     *
     * @return the last modify time
     */
    public Date getLastModifyTime() {
        return lastModifyTime;
    }

    /**
     * Gets last operator.
     *
     * @return the last operator
     */
    public Long getLastOperator() {
        return lastOperator;
    }

    /**
     * Sets id.
     *
     * @param id the id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * Sets supplier id.
     *
     * @param supplier the supplier
     */
    public void setSupplier (Supplier supplier) {
        this.supplier = supplier;
    }

    /**
     * Sets operation warehouse.
     *
     * @param operationWarehouse the operation warehouse
     */
    public void setOperationWarehouse(String operationWarehouse) {
        this.operationWarehouse = operationWarehouse;
    }

    /**
     * Sets operation dock.
     *
     * @param operationDock the operation dock
     */
    public void setOperationDock(String operationDock) {
        this.operationDock = operationDock;
    }

    /**
     * Sets operation platform.
     *
     * @param operationPlatform the operation platform
     */
    public void setOperationPlatform(String operationPlatform) {
        this.operationPlatform = operationPlatform;
    }

    /**
     * 
     * @param wareType
     */
    public void setWareType(WareType wareType) {
        this.wareType = wareType;
    }

    /**
     * Sets maintenance qualification.
     *
     * @param maintenanceQualification the maintenance qualification
     */
    public void setMaintenanceQualification(String maintenanceQualification) {
        this.maintenanceQualification = maintenanceQualification;
    }

    /**
     * Sets onsite staff list.
     *
     * @param onsiteStaffList the onsite staff list
     */
    public void setOnsiteStaffList(String onsiteStaffList) {
        this.onsiteStaffList = onsiteStaffList;
    }

    /**
     * Sets inspection permit.
     *
     * @param inspectionPermit the inspection permit
     */
    public void setInspectionPermit(String inspectionPermit) {
        this.inspectionPermit = inspectionPermit;
    }

    /**
     * Sets disabled.
     *
     * @param disabled the disabled
     */
    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    /**
     * Sets create time.
     *
     * @param createTime the create time
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * Sets creator.
     *
     * @param creator the creator
     */
    public void setCreator(Long creator) {
        this.creator = creator;
    }

    /**
     * Sets last modify time.
     *
     * @param lastModifyTime the last modify time
     */
    public void setLastModifyTime(Date lastModifyTime) {
        this.lastModifyTime = lastModifyTime;
    }

    /**
     * Sets last operator.
     *
     * @param lastOperator the last operator
     */
    public void setLastOperator(Long lastOperator) {
        this.lastOperator = lastOperator;
    }
}
